// 行索引 Math.floor(mid / n) 整除求行
// 列索引 mid % n 取余求列
var searchMatrix = function (matrix, target) {
    if (!matrix || !matrix.length || !matrix[0].length) return false
    const m = matrix.length
    const n = matrix[0].length
    let left = 0
    let right = m * n - 1
    while (left <= right) {
        const mid = Math.floor((left + right) / 2)
        const midValue = matrix[Math.floor(mid / n)][mid % n]
        if (midValue === target) {
            return true
        } else if (midValue < target) {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }
    return false
}

console.log(searchMatrix([[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], 3)); // true
console.log(searchMatrix([[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], 13)); // false
